package zongbao.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import zongbao.domain.Adim;
import zongbao.domain.Book;
import zongbao.domain.User;
import zongbao.service.AdimService;
import zongbao.service.BookService;
import zongbao.service.UserService;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/adim")
public class AdimController {
    @Autowired
    private AdimService adimService;
    @Autowired
    private BookService bookService;
    @Autowired
    private UserService userService;
    Adim login = null;
    Adim zhaohui=null;
    //登录
    @RequestMapping(value = "/login", produces = "text/html;charset=UTF-8")
    public String login(Adim adim, Model model, HttpSession session) {
        login = adimService.login(adim);
        if (login != null) {
            session.setAttribute("USER_SESSION", login);
            return "adim-main";
        } else {
            model.addAttribute("msg", "错误，重新登录");
            return "adim-login";
        }
    }
    //分页查看
    @RequestMapping("/fenye")
    public ModelAndView fenye(@RequestParam(defaultValue = "1")int page,@RequestParam(defaultValue = "3")int size){
        PageHelper.startPage(page,size);
        List<Book> list = bookService.AllBook();
        PageInfo<Book> pageInfo=new PageInfo<Book>(list);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("adim-fenye-book");
        modelAndView.addObject("list", list);
        modelAndView.addObject("pageInfo", pageInfo);
        return modelAndView;
    }
    //查看所有图书信息
    @RequestMapping("/Allbook")
    public ModelAndView findBookById() {
        List<Book> list = bookService.AllBook();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("adim-book");
        modelAndView.addObject("list", list);
        return modelAndView;
    }
    //修改图书信息
    @RequestMapping("/upbook")
    public ModelAndView upbook() {
        List<Book> list = bookService.AllBook();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("adim-upbook");
        modelAndView.addObject("list", list);
        return modelAndView;
    }
    @RequestMapping("/updatebook/{bookId}")
    public ModelAndView updatebook(@PathVariable("bookId") Integer id){
        Book book = bookService.findBookById(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("adim-book-up");
        modelAndView.addObject("book", book);
        return modelAndView;
    }
    @RequestMapping("/datebook/{bookId}")
    public String datebook(@PathVariable("bookId") Integer id,Book book){
        Book book1 = new Book();
        book1.setId(id);
        book1.setName(book.getName());
        book1.setAuthor(book.getAuthor());
        book1.setDetail(book.getDetail());
        book1.setPress(book.getPress());
        bookService.updatebook(book1);
        return "redirect:/adim/upbook";
    }
    //查看所有学生信息
    @RequestMapping("/user")
    public ModelAndView allUser() {
        List<User> users = userService.allUser();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("adim-user");
        modelAndView.addObject("list", users);
        return modelAndView;
    }
    //修改学生信息
    @RequestMapping("/upuser")
    public ModelAndView upuser() {
        List<User> users = userService.allUser();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("adim-upuser");
        modelAndView.addObject("list", users);
        return modelAndView;
    }
    @RequestMapping("/updateuser/{userId}")
    public ModelAndView updateuser(@PathVariable("userId") Integer id){
        User user= userService.findUserById(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("adim-user-up");
        modelAndView.addObject("user", user);
        return modelAndView;
    }
    @RequestMapping("/dateuser/{userId}")
    public String dateuser(@PathVariable("userId") Integer id,User user){
        User user1 = new User();
        user1.setId(id);
        user1.setJiebook(user.getJiebook());
        user1.setUsername(user.getUsername());
        user1.setWeigui(user.getWeigui());
        user1.setAgs(user.getAgs());
        user1.setSax(user.getSax());
        user1.setCollege(user.getCollege());
        user1.setBanji(user.getBanji());
        userService.updateuser(user1);
        return "redirect:/adim/upuser";
    }
    //添加学生
    @RequestMapping("/inuser")
    public ModelAndView inUser() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("adim-user-add");
        return modelAndView;
    }
    @RequestMapping("/adduser")
    public String adduser(User user){
        userService.adduser(user);
        return "redirect:/adim/upuser";
    }
    //删除学生信息
    @RequestMapping("/del/{userId}")
    public String del(@PathVariable("userId") Integer id){
        userService.del(id);
        return "redirect:/adim/user";
    }
    //查看归还图书
    @RequestMapping("/book")
    public ModelAndView guiBook() {
        List<Book> books = bookService.guiBook();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("adim-guibook");
        modelAndView.addObject("list", books);
        return modelAndView;
    }
    //确定图书归还
    @RequestMapping("/quehuan/{bookId}")
    public String quehuan(@PathVariable("bookId") Integer id){
        Book book = new Book();
        book.setId(id);
        book.setUserid(0);
        book.setUsertime(null);
        book.setBooktime(null);
        book.setGuihuan("可借");
        bookService.quehuan(book);
        return "redirect:/adim/book";
    }
    //添加图书
    @RequestMapping("/inbook")
    public ModelAndView inBook() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("adim-book-add");
        return modelAndView;
    }
    @RequestMapping("/addbook")
    public String addbook(Book book){
        bookService.addbook(book);
        return "redirect:/adim/Allbook";
    }
    //删除图书
    @RequestMapping("/detabook/{bookId}")
    public String detabook(@PathVariable("bookId") Integer id){
        bookService.detabook(id);
        return "redirect:/adim/Allbook";
    }
    //修改密码
    @RequestMapping("/pwd")
    public ModelAndView pwd() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("adim-pwd");
        return modelAndView;
    }
    @RequestMapping("/pwod")
    public String pwod(@RequestParam("pwd") String pwd, Adim adim, Model model){
        if (adim.getPassword().equals(login.getPassword())){
            Adim adim1 = new Adim();
            adim1.setPassword(pwd);
            adim1.setId(login.getId());
            adimService.updatepwd(adim1);
            model.addAttribute("msg","密码修改成功，请重新登录");
            return "adim-login";
        }else {
            model.addAttribute("msg","旧密码错误，请重新输入");
            return "redirect:/adim/pwd";
        }
    }
    //找回密码
    //忘记密码
    @RequestMapping("/wm")
    public ModelAndView wm(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("adim-wm");
        return modelAndView;
    }
    @RequestMapping("/zhaohui")
    public String zhaohui(Adim adim, Model model){
        zhaohui = adimService.zhaohui(adim);
        if (zhaohui!=null){
            return "adim-zhaohui";
        }else {
            model.addAttribute("msg","信息有误，请重新输入");
            return "redirect:/adim/wm";
        }
    }
    @RequestMapping("/dao")
    public String xiugai(Adim adim, Model model){
        Adim adim1 = new Adim();
        adim1.setPassword(adim.getPassword());
        adim1.setId(zhaohui.getId());
        adimService.updatepwd(adim1);
        model.addAttribute("msg","重置密码成功，请重新登录");
        return "adim-login";
    }
    //注册
    //注册
    @RequestMapping("/inadd")
    public ModelAndView inadim() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("adim-inadd");
        return modelAndView;
    }
    @RequestMapping("/add")
    public String add(Adim adim,Model model){
        adimService.addadim(adim);
        model.addAttribute("msg","注册成功，请重新登录");
        return "adim-login";
    }
    //数据可视化
    @RequestMapping("/coll")
    public String college(Model model){
        //学院
        List<User> 火烧靖国神厕学院 = userService.college("火烧靖国神厕学院");
        int a=火烧靖国神厕学院.size();
        model.addAttribute("aa",a);
        List<User> 干死小日本学院 = userService.college("干死小日本学院");
        int b=干死小日本学院.size();
        model.addAttribute("bb",b);
        List<User> 刺杀拜登学院 = userService.college("刺杀拜登学院");
        int c=刺杀拜登学院.size();
        model.addAttribute("cc",c);
        List<User> 马踏东京学院 = userService.college("马踏东京学院");
        int d=马踏东京学院.size();
        model.addAttribute("dd",d);
        List<User> 红旗满天下学院 = userService.college("红旗满天下学院");
        int e=红旗满天下学院.size();
        model.addAttribute("ee",e);
        //性别
        List<User> 男 = userService.Sax("男");
        int f=男.size();
        model.addAttribute("ff",f);
        List<User> 女 = userService.Sax("女");
        int g=女.size();
        model.addAttribute("gg",g);
        List<User> users = userService.allUser();
        int y=users.size();
        model.addAttribute("yy",y);
        //班级
        List<User> 商务班 = userService.Banji("商务班");
        List<User> 电信班 = userService.Banji("电信班");
        List<User> 软工班 = userService.Banji("软工班");
        List<User> 工程班 = userService.Banji("工程班");
        int h=商务班.size();
        int i=电信班.size();
        int j=软工班.size();
        int k=工程班.size();
        model.addAttribute("hh",h);
        model.addAttribute("ii",i);
        model.addAttribute("jj",j);
        model.addAttribute("kk",k);
        //借书情况
        List<Book> 可借 = bookService.Tvbook("可借");
        List<Book> 已借 = bookService.Tvbook("已借");
        List<Book> tvbook = bookService.Tvbook("已归还,待管理员确定");
        List<Book> books = bookService.AllBook();
        int o=可借.size();
        int p=已借.size();
        int q=tvbook.size();
        int u=books.size();
        model.addAttribute("oo",o);
        model.addAttribute("pp",p);
        model.addAttribute("qq",q);
        model.addAttribute("uu",u);
        return "adim-look-user";
    }
    //搜索框
    @RequestMapping("/sousuo")
    public ModelAndView Sousuo(User user){
        Map<Object,Object> map=new HashMap<>();
        map.put("id",user.getId());
        map.put("username",user.getUsername());
        map.put("sax",user.getSax());
        map.put("banji",user.getBanji());
        map.put("college",user.getCollege());
        List<User> sousuo = userService.Sousuo(map);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("adim-user");
        modelAndView.addObject("list", sousuo);
        return modelAndView;
//        userService.adduser(user);
//        return "redirect:/adim/user";
    }
    //退出登录
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        //清除Session
        session.invalidate();
        //退出登录后重定向到登录页面
        return "adim-login";
    }
}